var config = require('./config.js'),
	express = require('express'),
	mysql = require('mysql'),
	bodyParser = require("body-parser"); ;

var check = function(options){
					var checkEmail = function(email){
						var reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/

						if(!reg.test(email)){

							messageBox.show('您输入的邮箱有误，请检查后再输入',false);
							return false;
						}
					}

					var checkPhone = function(phone){
						var reg = /^1\d{10}$/

						if(phone.match(reg)){
							
						}else{

							messageBox.show('您输入的手机号码有误，请检查后再输入',false);
							return false;
						}
					}

					var reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/

					if(!reg.test(options['email'])){

						messageBox.show('您输入的邮箱有误，请检查后再输入',false);
						return false;
					}

					var phone_reg = /^1\d{10}$/

					if(!phone_reg.test(options['mobile'])){
						messageBox.show('您输入的手机号码有误，请检查后再输入',false);
						return false;
					}

					if(!options['name']){
						messageBox.show('您输入的姓名为空，请检查后再输入',false);
						return false;
					}

					if(!options['nation']){
						messageBox.show('您输入的民族为空，请检查后再输入',false);
						return false;
					}

					if(!options['hobby']){
						messageBox.show('您输入的爱好为空，请检查后再输入',false);
						return false;
					}

					if(!options['address']){
						messageBox.show('您输入的宿舍为空，请检查后再输入',false);
						return false;
					}

					if(!options['organization']){
						messageBox.show('您是否曾经加入过其他组织，如果没有请填写无，不能为空，请检查后再输入',false);
						return false;
					}

					if( typeof options['technoligy'] === 'undefined'){
						messageBox.show('您输入的技术倾向为空，请检查后再输入',false);
						return false;
					}
					if( typeof options['apartment'] === 'undefined') {
						messageBox.show('您输入的想加入的部门为空，请检查后再输入',false);
						return false;
					}
					if( typeof options['agree_to_allocation'] === 'undefined'){
						messageBox.show('您还没有填写是否服从分配，请检查后再输入',false);
						return false;
					}
					if(!options['await'] ){
						messageBox.show('您输入的俱乐部期待为空，请检查后再输入',false);
						return false;
					}
					if(!options['plan']){
						messageBox.show('您输入的大学规划为空，请检查后再输入',false);
						return false;
					}
					if(!options['assess']){
						messageBox.show('您输入的自我评价为空，请检查后再输入',false);
						return false;
					}
					return true;
				}


var app = express();

app.use(bodyParser.urlencoded({ extended: false })); 

app.post("/regist",function(req,res){
	var argv = req.body;

	console.log(req.body);

	if(!check(argv)){
		res.send('{"status":2}');
		res.end();
	}else{

		var connection = mysql.createConnection(config.mysql);

		connection.connect();

		var sql = "insert into regist_table(name, politics, sex, nation, mobile, email, hobby, address, organization, technoligy, apartment, agree_to_allocation, await, plan, assess) values('?','?','?','?','?','?','?','?','?','?','?','?','?','?','?')";

		for(var key in argv){
			sql = sql.replace('?',argv[key]);
		}

		console.log(sql);

		connection.query(sql, function(err,rows,fields){
			if(err) {
				res.send('{"status":2}');
				
			}
			else{
				res.send('{"status":1}');
			}
			res.end();
		});

		connection.end();
	}
		
});

app.use(express.static(__dirname + config.web.appDir));

app.listen(config.web.port);